<?php
class Search_Form_MaidRefineForm extends Ac_Form
{
    private $settings;

    protected $elementDecorators = array(
        'ViewHelper',
        'Errors',
       // 'Description'
        array('Label', array()),
        //array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class'=>'inputText')),
    );

    protected $elementSelectDecorators = array(
        'ViewHelper',
        'Errors',
       // 'Description'
        array('Label', array()),
        array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class'=>'select')),
    );

    protected $formDecorators = array(
        'FormElements',
  //      array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')),
        array('Description', array('placement' => 'prepend')),
        'Form',

    );

    protected $elementCheckBoxDecorators = array(
        'ViewHelper',
        'Errors',
       // 'Description'
        //array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        array('Label', array('placement' => 'APPEND')),
        array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class'=>'checkbox')),
    );

    protected $elementMultiCheckBoxDecorators = array(
        'ViewHelper',
        'Errors',
       // 'Description'
        //array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        array('Label', array('tag' => 'span', 'class' => 'multicheckbox-label')),
    );    

    protected $displayGroupDecorators = array(
        'FormElements',
        //array(array('innerHTmlTag' => 'HtmlTag'), array('tag' => 'div', 'class' => 'checkbox'))
        //array('HtmlTag', array('tag' => 'dl')),
    );


    protected $subFormDecorators = array(
        'FormElements',
        array('Description', array('placement' => 'prepend')),
    );


    protected $elementButtonDecorators = array(
        'ViewHelper',
        'Errors',
       // 'Description'
        //array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        //array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class'=>'button')),
        //array('HtmlTag', array('class' => 'inputText')),
    );

    public function __construct($options = array())
    {
        $this->settings = (object) (!empty($options['settings']) ? $options['settings'] : array());
        parent::__construct($options);
    }

    protected function additionalInformation()
    {
        $form1 = $this->getNewSubForm();

        $deco = array_merge($this->elementMultiCheckBoxDecorators, array(array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class'=>'checkbox household_and_cleaning househeld_care'))) );
        //var_dump($deco);exit;
        // Household and Cleaning
        
        $form1->addElement('multiCheckbox', 'key_search_tool_householdcleaning', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $deco,
            'multiOptions' => Model_Listing_Maid::getHouseholdCleaning(),
            'required'     => false,
            'attribs'      => array(array('id' => 'cooking')),
            'label'        => 'Household Care',
            'value'        => $this->getSetting('householdcleaning') == null ? 'dasdasdasd' : $this->getSetting('householdcleaning'),
        ));
        
        $deco = array_merge($this->elementMultiCheckBoxDecorators, array(array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class'=>'checkbox household_and_cleaning cooking_style'))) );
        // cooking Style
        $form1->addElement('multiCheckbox', 'key_search_tool_cooking', array(
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $deco,
            'multiOptions' => Model_Listing_Maid::getCookingStyle(),
            'required'     => false,
            'label'        => 'Cooking Style',
            'value'        => $this->getSetting('cooking'),
        ));

        $deco = array_merge($this->elementMultiCheckBoxDecorators, array(array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class'=>'checkbox household_and_cleaning people_care'))) );
        // People Care and Nursing
        $form1->addElement('multiCheckbox', 'key_search_tool_care_nursing', array(
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $deco,
            'multiOptions' => Model_Listing_Maid::getPeopleCareNursingSearch(),
            'required'     => false,
            'label'        => 'People Care',
            'value'        => $this->getSetting('care_nursing'),
        ));

        $form1->addDecorator('fieldset', array('class' => 'optional', 'style' => 'display:none'));
        $form1->setLegend('Additional Features');
        $this->addSubForm($form1, 'optional');        
    }

    protected function personalinformation()
    {
        $form1 = $this->getNewSubForm();


        // Education
        $form1->addElement('select', 'key_search_tool_education', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $this->elementSelectDecorators,
            'multiOptions' => Model_Listing_Maid::getEducation(),
            'required'     => false,
            'label'        => 'Education',
            'value'        => $this->getSetting('education'),
        ));
        
        // Languages
        $form1->addElement('select', 'key_search_tool_languages', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $this->elementSelectDecorators,
            'multiOptions' => Model_Listing_Maid::getLanguages(),
            'required'     => false,
            'label'        => 'Languages',
            'value'        => $this->getSetting('languages'),
        ));

        
        // Marital Status
        $form1->addElement('select', 'key_search_tool_maritalstatus', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $this->elementSelectDecorators,
            'multiOptions' => Model_Listing_Maid::getMaritalStatus(),
            'required'     => false,
            'label'        => 'Marital Status',
            'value'        => $this->getSetting('maritalstatus'),
        ));

        // Nationality
        $form1->addElement('select', 'key_search_tool_nationality', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $this->elementSelectDecorators,
            'multiOptions' => Model_Listing_Maid::getNationality(),
            'required'     => false,
            'label'        => 'Nationality',
            'value'        => $this->getSetting('nationality'),
        ));

        // Gender
        $form1->addElement('select', 'key_search_tool_gender', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $this->elementSelectDecorators,
            'multiOptions' => Model_Listing_Maid::getGenderSearch(),
            'required'     => false,
            'label'        => 'Gender',
            'value'        => $this->getSetting('gender'),
        ));
        

        // Visa Status
        $form1->addElement('select', 'key_search_tool_visastatus', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $this->elementSelectDecorators,
            'multiOptions' => Model_Listing_Maid::getVisaStatus(),
            'required'     => false,
            'label'        => 'Visa Status',
            'value'        => $this->getSetting('visastatus'),
        ));
        

        // Employment Type
        $form1->addElement('select', 'key_search_tool_employmenttype', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $this->elementSelectDecorators,
            'multiOptions' => Model_Listing_Maid::getEmploymentType(),
            'required'     => false,
            'label'        => 'Employment Type',
            'value'        => $this->getSetting('employmenttype'),
        ));     
        

        // Age
        $form1->addElement('jqslider', 'key_search_tool_age', array(
                    'jQueryParams' => array(
                        'min'      => $this->getSetting('age_min') !== null ? $this->getSetting('age_min') : 18,
                        'max'      => $this->getSetting('age_max') !== null ? $this->getSetting('age_max') : 50,
                        'values'   => $this->getSetting('age_start') !== null ? $this->getSetting('age_start') : array('18', '50'),
                        'currency' => 'year',
                        'range'    => true,
                        'step'     => 1,
                        'change'   => 'refineSearch()',
                        ),
                    'label'       => 'Age',
                    'decorators' => array(
                            'Errors',
                            'UiWidgetElement',
                            array('Label', array('escape' => false)),
                            array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'slide')),
                    )
                )
        );        
        
/*
        // Religion
        $form1->addElement('select', 'key_search_tool_religion', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $this->elementSelectDecorators,
            'multiOptions' => Model_Listing_Maid::getReligion(),
            'required'     => false,
            'label'        => 'Religion',
            'value'        => $this->getSetting('religion'),
        ));

        // Chinese Zodiac
        $form1->addElement('select', 'key_search_tool_chinesezodiac', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $this->elementSelectDecorators,
            'multiOptions' => Model_Listing_Maid::getChineseZodiac(),
            'required'     => false,
            'label'        => 'Chinese Zodiac',
            'value'        => $this->getSetting('chinesezodiac'),
        ));

        // Horoscope
        $form1->addElement('select', 'key_search_tool_horoscope', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $this->elementSelectDecorators,
            'multiOptions' => Model_Listing_Maid::getHoroscope(),
            'required'     => false,
            'label'        => 'Horoscope',
            'value'        => $this->getSetting('horoscope'),
        ));




        // Height
        $form1->addElement('jqslider', 'key_search_tool_height', array(
                    'jQueryParams' => array(
                        'min'      => $this->getSetting('height_min') !== null ? $this->getSetting('height_min') : 135,
                        'max'      => $this->getSetting('height_max') !== null ? $this->getSetting('height_max') : 175,
                        'values'   => $this->getSetting('height_start') !== null ? $this->getSetting('height_start') : array('135', '175'),
                        'currency' => 'cm',
                        'range'    => true,
                        'step'     => 1,
                        'change'   => 'refineSearch()',
                        ),
                    'label'       => 'Height (cm)',
                    'decorators' => array(
                            'Errors',
                            'UiWidgetElement',
                            array('Label', array('escape' => false)),
                            array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'slide')),
                    )
                )
        );

        // Weight
        $form1->addElement('jqslider', 'key_search_tool_weight', array(
                    'jQueryParams' => array(
                        'min'      => $this->getSetting('weight_min') !== null ? $this->getSetting('weight_min') : 30,
                        'max'      => $this->getSetting('weight_max') !== null ? $this->getSetting('weight_max') : 65,
                        'values'   => $this->getSetting('weight_start') !== null ? $this->getSetting('weight_start') : array('30', '65'),
                        'currency' => 'kg',
                        'range'    => true,
                        'step'     => 1,
                        'change'   => 'refineSearch()',
                        ),
                    'label'       => 'Weight (kg)',
                    'decorators' => array(
                            'Errors',
                            'UiWidgetElement',
                            array('Label', array('escape' => false)),
                            array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'slide')),
                    )
                )
        );
*/
        

        $view = Zend_Registry::get('Zend_View');
        // --> Additional Button
        $form1->addElement('hidden', 'additional_button', array(
            'decorators'  => array(
                    'Errors',
                    'ViewHelper',
                    array('Description', array('escape' => false, 'placement' => 'PREPEND', 'tag' => 'div', 'class' => 'desc more', 'id' => 'additional_btn')),
            ),
            'Description' => '<a href="#">'.$view->t('Additional Features').' »</a>',
            'value'       => $this->getSetting('key_search_tool_district'),
        ));
        
        $form1->addDecorator('fieldset', array('class' => 'key_search'));
        $form1->setLegend('Personal Information');
        $this->addSubForm($form1, 'personal');
    }

    public function init()
    {
        parent::init();

        $this->setSubFormDecorators($this->subFormDecorators);
        $this->setDisplayGroupDecorators($this->displayGroupDecorators);

        $this->setMethod('GET');
        $this->setName('RefineSearch');

        $element = new Zend_Form_Element_Hidden('keyword');
        if (isset($_GET['keyword'])) {$element->setValue($_GET['keyword']);}
        $element->setDecorators(array(
                'ViewHelper'
            )
        );
        $this->addElement($element);

        $element = new Zend_Form_Element_Hidden('small_category_list');
        if (Zend_Registry::isRegistered('category_list_selected')) {
            $selected = Zend_Registry::get('category_list_selected');
            $element->setValue($selected);
        } elseif (!empty($_GET['small_category_list'])) {
            $selected = $_GET['small_category_list'];
            $element->setValue($selected);
        }
        $element->setDecorators(array(
                'ViewHelper'
            )
        );
        $this->addElement($element);

        $this->personalinformation();
        $this->additionalInformation();
        
        // Submit Button
        $this->addElement('button', 'v', array(
            'required'    => false,
            'ignore'      => true,
            'type'        => 'submit',
            'label'       => 'Go',
            'decorators'  => $this->elementButtonDecorators,
        ));

        $this->addDisplayGroup(array('v'), 'key_search_tool2',
            array(
                'decorators' => array_merge($this->displayGroupDecorators, array(array('HtmlTag', array('tag' => 'div', 'class' => 'text button'))))
            )
        );        
    }

    function populate(array $values)
    {
        //Skill Level Western
        if (isset($values['work']['key_search_tool_skilllevelwestern'])) {
            $this->settings->skilllevelwestern = $values['work']['key_search_tool_skilllevelwestern'];
        }

        //Skill Level Chinese
        if (isset($values['work']['key_search_tool_skilllevelchinese'])) {
            $this->settings->skilllevelchinese = $values['work']['key_search_tool_skilllevelchinese'];
        }

        //Household and Cleaning
        if (isset($values['work']['key_search_tool_householdcleaning'])&& isset($values['work-key_search_tool_householdcleaning-1'])) {
            $this->settings->householdcleaning = $values['work']['key_search_tool_householdcleaning'];
        }

        //Years of Experience Newborn
        if (isset($values['work-key_search_tool_yearsexperiencenewborn']) && isset($values['work-key_search_tool_yearsexperiencenewborn-1'])) {
            $this->settings->yearsexperiencenewborn_start = array($values['work-key_search_tool_yearsexperiencenewborn'], $values['work-key_search_tool_yearsexperiencenewborn-1']);
        }

        //Years of Experience Children
        if (isset($values['work-key_search_tool_yearsexperiencechildren']) && isset($values['work-key_search_tool_yearsexperiencechildren-1'])) {
            $this->settings->yearsexperiencechildren_start = array($values['work-key_search_tool_yearsexperiencechildren'], $values['work-key_search_tool_yearsexperiencechildren-1']);
        }

        //Years of Experience Disable
        if (isset($values['work-key_search_tool_yearsexperiencedisable']) && isset($values['work-key_search_tool_yearsexperiencedisable-1'])) {
            $this->settings->yearsexperiencedisable_start = array($values['work-key_search_tool_yearsexperiencedisable'], $values['work-key_search_tool_yearsexperiencedisable-1']);
        }

        //Years of Experience Elderly
        if (isset($values['work-key_search_tool_yearsexperienceelderly']) && isset($values['work-key_search_tool_yearsexperienceelderly-1'])) {
            $this->settings->yearsexperienceelderly_start = array($values['work-key_search_tool_yearsexperienceelderly'], $values['work-key_search_tool_yearsexperienceelderly-1']);
        }




        //Years of Experience in Hong Kong
        if (isset($values['work-key_search_tool_hongkongyearsexperience']) && isset($values['work-key_search_tool_hongkongyearsexperience-1'])) {
            $this->settings->hongkongyearsexperience_start = array($values['work-key_search_tool_hongkongyearsexperience'], $values['work-key_search_tool_hongkongyearsexperience-1']);
        }

        //Years of Experience in Indonesia
        if (isset($values['work-key_search_tool_indonesiayearsexperience']) && isset($values['work-key_search_tool_indonesiayearsexperience-1'])) {
            $this->settings->indonesiayearsexperience_start = array($values['work-key_search_tool_indonesiayearsexperience'], $values['work-key_search_tool_indonesiayearsexperience-1']);
        }

        //Years of Experience in Malaysia
        if (isset($values['work-key_search_tool_malaysiayearsexperience']) && isset($values['work-key_search_tool_malaysiayearsexperience-1'])) {
            $this->settings->malaysiayearsexperience_start = array($values['work-key_search_tool_malaysiayearsexperience'], $values['work-key_search_tool_malaysiayearsexperience-1']);
        }

        //Years of Experience in Middle East
        if (isset($values['work-key_search_tool_middleeastyearsexperience']) && isset($values['work-key_search_tool_middleeastyearsexperience-1'])) {
            $this->settings->middleeastyearsexperience_start = array($values['work-key_search_tool_middleeastyearsexperience'], $values['work-key_search_tool_middleeastyearsexperience-1']);
        }

        //Years of Experience in Philippines
        if (isset($values['work-key_search_tool_philippinesyearsexperience']) && isset($values['work-key_search_tool_philippinesyearsexperience-1'])) {
            $this->settings->philippinesyearsexperience_start = array($values['work-key_search_tool_philippinesyearsexperience'], $values['work-key_search_tool_philippinesyearsexperience-1']);
        }

        //Years of Experience in Singapore
        if (isset($values['work-key_search_tool_singaporeyearsexperience']) && isset($values['work-key_search_tool_singaporeyearsexperience-1'])) {
            $this->settings->singaporeyearsexperience_start = array($values['work-key_search_tool_singaporeyearsexperience'], $values['work-key_search_tool_singaporeyearsexperience-1']);
        }

        //Years of Experience in Taiwan
        if (isset($values['work-key_search_tool_taiwanyearsexperience']) && isset($values['work-key_search_tool_taiwanyearsexperience-1'])) {
            $this->settings->taiwanyearsexperience_start = array($values['work-key_search_tool_taiwanyearsexperience'], $values['work-key_search_tool_taiwanyearsexperience-1']);
        }

        //Years of Experience in Thailand
        if (isset($values['work-key_search_tool_thailandyearsexperience']) && isset($values['work-key_search_tool_thailandyearsexperience-1'])) {
            $this->settings->thailandyearsexperience_start = array($values['work-key_search_tool_thailandyearsexperience'], $values['work-key_search_tool_thailandyearsexperience-1']);
        }

        //Years of Experience in Other
        if (isset($values['work-key_search_tool_otheryearsexperience']) && isset($values['work-key_search_tool_otheryearsexperience-1'])) {
            $this->settings->otheryearsexperience_start = array($values['work-key_search_tool_otheryearsexperience'], $values['work-key_search_tool_otheryearsexperience-1']);
        }

        //Education
        if (isset($values['personal']['key_search_tool_education'])) {
            $this->settings->education = $values['personal']['key_search_tool_education'];
        }
        
        //Languages
        if (isset($values['personal']['key_search_tool_languages'])) {
            $this->settings->languages = $values['personal']['key_search_tool_languages'];
        }

        //Marital Status
        if (isset($values['personal']['key_search_tool_maritalstatus'])) {
            $this->settings->maritalstatus = $values['personal']['key_search_tool_maritalstatus'];
        }

        //Nationality
        if (isset($values['personal']['key_search_tool_nationality'])) {
            $this->settings->nationality = $values['personal']['key_search_tool_nationality'];
        }

        //Gender
        if (isset($values['personal']['key_search_tool_gender'])) {
            $this->settings->gender = $values['personal']['key_search_tool_gender'];
        }

        //Religion
        if (isset($values['personal']['key_search_tool_religion'])) {
            $this->settings->religion = $values['personal']['key_search_tool_religion'];
        }

        //Chinese Zodiac
        if (isset($values['personal']['key_search_tool_chinesezodiac'])) {
            $this->settings->chinesezodiac = $values['personal']['key_search_tool_chinesezodiac'];
        }

        //Horoscope
        if (isset($values['personal']['key_search_tool_horoscope'])) {
            $this->settings->horoscope = $values['personal']['key_search_tool_horoscope'];
        }

        //Visa Status
        if (isset($values['personal']['key_search_tool_visastatus'])) {
            $this->settings->visastatus = $values['personal']['key_search_tool_visastatus'];
        }

        //Employment Type
        if (isset($values['personal']['key_search_tool_employmenttype'])) {
            $this->settings->employmenttype = $values['personal']['key_search_tool_employmenttype'];
        }

        //Age
        if (isset($values['personal-key_search_tool_age']) && isset($values['personal-key_search_tool_age-1'])) {
            $this->settings->age_start = array($values['personal-key_search_tool_age'], $values['personal-key_search_tool_age-1']);
        }

        //Weight
        if (isset($values['personal-key_search_tool_weight']) && isset($values['personal-key_search_tool_weight-1'])) {
            $this->settings->weight_start = array($values['personal-key_search_tool_weight'], $values['personal-key_search_tool_weight-1']);
        }

        //Height
        if (isset($values['personal-key_search_tool_height']) && isset($values['personal-key_search_tool_height-1'])) {
            $this->settings->height_start = array($values['personal-key_search_tool_height'], $values['personal-key_search_tool_height-1']);
        }
    }

    private function getNewSubForm()
    {
        $form = new Ac_SubForm();
        $form->setDecorators($this->subFormDecorators);
        $form->setDisplayGroupDecorators($this->displayGroupDecorators);
        return $form;
    }

    private function getSetting($name)
    {
        if (!empty($this->settings->$name)) {
            return $this->settings->$name;
        }
        return null;
    }
}